package com.shenyong.controller;

import com.shenyong.entity.Article;
import com.shenyong.entity.Article_Kind;

import com.shenyong.entity.Collection;
import com.shenyong.entity.Comment;
import com.shenyong.exception.MyException;
import com.shenyong.ladmin.result.TinyURL;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.shenyong.service.QArticleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/qArticle")
public class QArticleController {
    @Autowired
    private QArticleService qArticleService;

    /*  @Value("${imageFilePath}")
      private String imageFilePath;*/
    @RequestMapping("addArticle")
    public int addArticle(Article article) {
        article.setArticle_uid(6);
        TinyURL tinyURL = new TinyURL();
        article.setArticle_url(tinyURL.getLongStrUrl());
        if (article.getArticle_original().equals("1")) {
            article.setArticle_original("是");
        } else {
            article.setArticle_original("否");
        }
        return qArticleService.addArticle(article);
        /*if (!article.equals("")) {
            ,@RequestParam("imgpath")MultipartFile file
            // 获取文件名
            String fileName = file.getOriginalFilename();
                *//* 获取文件的后缀名 *//*
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            try {
                String newFileName = DateUtil.getCurrentDateStr() + suffixName;
                FileUtils.copyInputStreamToFile(file.getInputStream(), new File(imageFilePath + newFileName));
            } catch (Exception e) {
                System.out.println(e);
            }
        }*/
    }

    @RequestMapping("selectArticle_Kind")
    public List<Article_Kind> selectArticle_Kind() {
        return qArticleService.selectAllArticle_Kind();
    }

    @RequestMapping("newHotArticle")
    public List<Article> newHotArticle(Article article) {
        System.out.println(article);
        return qArticleService.newHotArticle(article);
    }

    @RequestMapping("addComment")
    public int addComment(Comment comment) {
        return qArticleService.addComment(comment);
    }

    @RequestMapping("artprev")
    public Object artprev(Article article){
        if(qArticleService.artprev(article)==null){
            return 0;
        }
        System.out.println(qArticleService.artprev(article));
        return qArticleService.artprev(article);
    }

    @RequestMapping("artnext")
    public Object artnext(Article article){
        if(qArticleService.artnext(article)==null){
            return 0;
        }
        return qArticleService.artnext(article);
    }
    @RequestMapping("uppageView")
    public int uppageView(Article article){
        return qArticleService.uppageView(article);
    }
    @RequestMapping("upcollect")
    public int upcollect(Collection collection){
        return qArticleService.upcollect(collection);
    }
    @RequestMapping("decollect")
    public int decollect(Collection collection){
        return qArticleService.decollect(collection);
    }
}
